package cate.game.role.res.leader.hallows;

import cate.common.table.d.GD;
import cate.common.table.d.GDFunc;
import cate.common.table.hallow.HallowHallucinateRow;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.res.leader.hallows.msg.HallowsActiveSkinResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@NoteClass(value = "圣器皮肤")
public class HallowsSkinPO extends FuncOpenChecker {

  @NoteField(value = "皮肤对应激活后过期时间", detail = "")
  public Map<Integer, Long> skinMap;

  public HallowsSkinPO() {
  }

  @Override
  public void initialize(Role role) {
    super.initialize(role);
    if (skinMap == null) {
        skinMap = new ConcurrentHashMap<>();
    }
    resetCheckTime();
  }

  @Transient
  @JsonIgnore
  public long checkTime;

  public void resetCheckTime() {
    checkTime = 0;
    for (long expireTime : skinMap.values()) {
      if (expireTime > 0 && (checkTime == 0 || checkTime > expireTime)) {
        checkTime = expireTime;
      }
    }
  }

  @Override
  public int getFuncId() {
    return GDFunc.HALLOW_SKIN;
  }

  @Override
  public void onAfterOpen() {

  }


  public GameResult hallucinate(int type, int skinId, Hallows hallows) {
    GameResult r = new GameResult();
    Hallow hallow = hallows.getItem(type);
    if (skinMap == null || !skinMap.containsKey(skinId)) {
      return r.fail("请先激活皮肤");
    }
    for (Hallow item : hallows.items) {
      if (item.skinId == skinId) {
        item.skinId = GD.INT_0;
      }
    }
    GameResult<Void> res = hallow.hallucinate(skinId);
    if (!res.ok()) {
      return res;
    }
    return r.success();
  }

  public GameResult activeSkin(int skinId) {
    GameResult<Void> result = new GameResult<>();
    HallowHallucinateRow hallucinateRow = role.getGame().table.hallow.hallucinate.get(skinId);
    if (hallucinateRow == null) {
      return result.fail("不存在该皮肤");
    }
    MixRes cost = new MixRes(hallucinateRow.consumeStr);
    GameResult<MixResItem> consume_r = cost.consume(role, null);
    if (!consume_r.ok()) {
      role.getGame().notice.tipResItem(role, consume_r.data);
      return result.fail(consume_r.message);
    }
    long hallucinationTime = skinMap.getOrDefault(skinId,0L);
    long nowTime = System.currentTimeMillis();
    if (hallucinationTime > nowTime) {
      hallucinationTime += hallucinateRow.activeTime * 1000L;
    } else {
      hallucinationTime = nowTime + hallucinateRow.activeTime * 1000L;
    }
    skinMap.put(skinId, hallucinationTime);
    noticeSkinStatus(this.skinMap, skinId);
    resetCheckTime();
    return result.success();
  }

  private void noticeSkinStatus(Map<Integer, Long> skinMap, int skinId) {
    role.sendNow(new HallowsActiveSkinResp(skinMap, skinId));
  }
}
